//
//  LoginViewController.m
//  VmiSJ
//
//  Created by 汪的一声就哭了！ on 2017/1/5.
//  Copyright © 2017年 汪的一声就哭了！. All rights reserved.
//

#import "LoginViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *headBackImg;
@property (strong, nonatomic) IBOutlet UIImageView *headImg;
@property (strong, nonatomic) IBOutlet UIButton *loginBtn;
@property (strong, nonatomic) IBOutlet UILabel *zhLabel;
@property (strong, nonatomic) IBOutlet UITextField *zhTF;
@property (strong, nonatomic) IBOutlet UILabel *mimaLabel;
@property (strong, nonatomic) IBOutlet UITextField *mimaTF;

@end

@implementation LoginViewController
- (IBAction)loginAction:(UIButton *)sender {
    [self.view endEditing:YES];
    if ([self.zhTF.text isEqualToString:@""]) {
        [[SQShowMessage shareMessage] showMessageWithStr:@"请输入手机号"];
    } else if ([self.mimaTF.text isEqualToString:@""]) {
        [[SQShowMessage shareMessage] showMessageWithStr:@"请输入密码"];
    } else {
        [self obtainLoginData];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册账号" style:UIBarButtonItemStylePlain target:self action:@selector(zhuceAction:)];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:self.fontSize],NSForegroundColorAttributeName:[UIColor colorWithRed:144.0/255.0 green:145.0/255.0 blue:146.0/255.0 alpha:1]} forState:UIControlStateNormal];
    
    self.headBackImg.layer.cornerRadius = WIDTH*5/18/2;
    self.headImg.layer.cornerRadius = WIDTH*5/18*9/10/2;
    self.loginBtn.layer.cornerRadius = (WIDTH-60)*37/260/2;
    self.zhLabel.font = [UIFont systemFontOfSize:self.fontSize];
    self.zhTF.font = [UIFont systemFontOfSize:self.fontSize];
    self.mimaLabel.font = [UIFont systemFontOfSize:self.fontSize];
    self.mimaTF.font = [UIFont systemFontOfSize:self.fontSize];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    tap.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardDidShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardDidHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}
#pragma mark ------------- 忘记密码
- (IBAction)wjmmAction:(UIButton *)sender {
    ForgetMimaViewController *viewC = [[ForgetMimaViewController alloc] init];
    [self.navigationController pushViewController:viewC animated:YES];
}
#pragma mark ------------- 注册
- (void)zhuceAction:(UIBarButtonItem *)btn
{
    RegisterViewController *viewC = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:viewC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -------------- textfield代理
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == self.zhTF) {
        [self.zhTF resignFirstResponder];
        [self.mimaTF becomeFirstResponder];
    }
    else {
        [self.view endEditing:YES];
    }
    return YES;
}
#pragma mark -------------- 登陆网络请求
- (void)obtainLoginData
{
    [NetRequiest handleDataWithString:[NSString stringWithFormat:@"%@",HTTPLOAD] dictionary:@{@"v1":@"login",@"phone":self.zhTF.text,@"password":self.mimaTF.text,@"push_id":RegistrationID} HUD:YES block:^(id responseObject) {
        if ([responseObject[@"code"] isEqual:@200]) {
            [[NSUserDefaults standardUserDefaults] setObject:responseObject[@"result"][@"uid"] forKey:@"U_id"];
            AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [app setMainRoot];
        } else {
            [[SQShowMessage shareMessage] showMessageWithStr:responseObject[@"msg"]];
        }
    }];
}
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    [self.view endEditing:YES];
}
#pragma mark ------------- 键盘弹出收起通知中心
- (void)keyBoardDidShow:(NSNotification *)noti
{
    if (WIDTH < 330) {
        [UIView animateWithDuration:0.2 animations:^{
            if (self.zhTF.isFirstResponder) {
                self.view.center = CGPointMake(self.view.center.x, HEIGHT/2);
            } else {
                self.view.center = CGPointMake(self.view.center.x, HEIGHT/2-50);
            }
        }];
    }
}
- (void)keyBoardDidHide:(NSNotification *)noti
{
    [UIView animateWithDuration:0.2 animations:^{
        self.view.center = CGPointMake(self.view.center.x, HEIGHT/2);
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
